package com.wty.example.automic;

import com.wty.annotations.ThreadSafe;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;

/**
 * @Author wangtengyu
 * @Create 2018-05-16-9:24
 */
@Slf4j
@ThreadSafe
public class AtomicExample6 {
    private static AtomicReferenceFieldUpdater<AtomicExample6,String> updater=AtomicReferenceFieldUpdater.newUpdater(AtomicExample6.class,String.class,"str");
    @Getter
    private volatile String str="hello";

    public static void main(String[] args) {
        AtomicExample6 atomicExample6=new AtomicExample6();
        if(updater.compareAndSet(atomicExample6,"hello","hello wty")){
            log.info("success1");
            System.out.println(atomicExample6.getStr());
        }
        if(updater.compareAndSet(atomicExample6,"hello wty","hello wty2")){
            log.info("success2");
            System.out.println(atomicExample6.getStr());
        }else{
            log.info("error");
            System.out.println(atomicExample6.getStr());
        }

    }
}
